package com.chlitina.store.modules.cash.model;

import java.util.ArrayList;
import java.util.List;

import com.chlitina.store.common.utils.DataFormater;
import com.chlitina.store.modules.base.entity.StoreProduct;
import com.chlitina.store.modules.cash.service.PaymentUility;
import com.chlitina.store.modules.employee.entity.Employee;

public class StoreProductCart {
	private List<ProductCardPayment> availableProductCards = null;
	private List<AllCardPayment> availableAllCards = null;
	private ProductPayment payment;
	private StoreProduct storeProduct;
	private Employee employee;
	private float realCost;
	private float realCostTotal;
	private int count = 1;
	private boolean hasSet = false;

	private float totalRequired;
//	private float alreayPay;
//	private float leftToPay;
	private List<String> details = new ArrayList<String>();

	private void updateRealCost() {
		realCostTotal = 0;
		realCost = 0;
		if (payment == null) {
			return;
		}
		realCostTotal = getPayment().getRealTotalCost();
		realCost = realCostTotal / count;
	}

	public void updateDetails() {
		if (payment == null) {
			details.clear();
			float retailPrice = getRetailPrice();
			realCost=0;
			realCostTotal=0;
			details.add("赠品 ：【" + getProductName() + "】");
			details.add("" + retailPrice);
			details.add("" + count);
			if(retailPrice!=0){
				details.add(DataFormater.format(realCost/ retailPrice));
			}else{
				details.add(DataFormater.format(0));
			}
			details.add("" + (realCost));
			details.add("" + realCostTotal);
			details.add("赠品");
			return;
		}
		details.clear();
		float retailPrice = getRetailPrice();
		details.add("购买产品 ：【" + getProductName() + "】");
		details.add("" + retailPrice);
		details.add("" + count);
		if(retailPrice!=0){
			details.add(DataFormater.format(realCost/ retailPrice));
		}else{
			details.add(DataFormater.format(0));
		}
		details.add("" + (realCost));
		details.add("" + realCostTotal);
		details.add(payment.getDescription());
	}

	public float getRetailPrice() {
		if (storeProduct != null) {
			if (storeProduct.getProduct_type() == StoreProduct.TYPE_SELF) {
				return storeProduct.getStoreSelfProduct().getRetail_price();
			} else {
				return storeProduct.getProduct().getRetail_price();
			}
		}
		return 0F;
	}

	String getProductName() {
		if (storeProduct != null) {
			if (storeProduct.getProduct_type() == StoreProduct.TYPE_SELF) {
				return storeProduct.getStoreSelfProduct().getName();
			} else {
				return storeProduct.getProduct().getName();
			}
		}
		return "";
	}

	public void updatePayment() {
		totalRequired = 0;
		float price;
		if (storeProduct.getProduct_type() == StoreProduct.TYPE_SYS) {
			price = storeProduct.getProduct().getRetail_price();

		} else {
			price = storeProduct.getStoreSelfProduct().getRetail_price();
		}
		totalRequired = price * count;
//		if (payment == null) {
//			alreayPay = 0;
//			leftToPay = totalRequired;
//		} else {
//			alreayPay = realCostTotal;
//			leftToPay = 0;
//		}
		hasSet = (realCostTotal == totalRequired);
	}

	public StoreProduct getStoreProduct() {
		return storeProduct;
	}

	public void setStoreProduct(StoreProduct product) {
		this.storeProduct = product;
	}

	public int getCount() {
		return count;
	}

	public void setCount(int count) {
		this.count = count;
	}

	public Employee getEmployee() {
		return employee;
	}

	public void setEmployee(Employee employee) {
		this.employee = employee;
	}

	public ProductPayment getPayment() {
		return payment;
	}

	public boolean isHasSet() {
		return hasSet;
	}

	public float getTotalRequired() {
		return totalRequired;
	}

//	public float getAlreayPay() {
//		return alreayPay;
//	}
//
//	public float getLeftToPay() {
//		return leftToPay;
//	}

	public List<String> getDetails() {
		return details;
	}

	public void setPayment(ProductPayment payment) {
		this.payment = payment;
	}

	public List<ProductCardPayment> getAvailableProductCards() {
		return availableProductCards;
	}

	public void setAvailableProductCards(
			List<ProductCardPayment> availableProductCards) {
		this.availableProductCards = availableProductCards;
	}

	public List<AllCardPayment> getAvailableAllCards() {
		return availableAllCards;
	}

	public void setAvailableAllCards(List<AllCardPayment> availableAllCards) {
		this.availableAllCards = availableAllCards;
	}

	public void calculatePayment(Member member) {
		updatePayment();
		CardPayment cardPayment = PaymentUility.findBestProductCard(member
				.getAvaibleProductCards(), getStoreProduct().getId()
				.longValue(), totalRequired);
		payment = new ProductPayment();
		if (cardPayment != null) {
			ProductCardPaymentDetail detail = new ProductCardPaymentDetail();
			detail.setCurrentLeftMoney(cardPayment.getCurrentMoney());
			detail.setRealCost(cardPayment.getRealCost());
			detail.setCardPayment(cardPayment);
			cardPayment.payMoney();
			payment.setProductCardPaymentDetail(detail);
			updateRealCost();
			payment.updateDescription();
			return;
		}
		cardPayment = PaymentUility.findBestAllCardForProduct(
				member.getAvaibleAllCards(), totalRequired);
		if (cardPayment != null) {
			AllCardPaymentDetail detail = new AllCardPaymentDetail();
			detail.setCurrentLeftMoney(cardPayment.getCurrentMoney());
			detail.setRealCost(cardPayment.getRealCost());
			detail.setCardPayment(cardPayment);
			cardPayment.payMoney();
			payment.setAllCardPaymentDetail(detail);
			updateRealCost();
			payment.updateDescription();
			return;
		}
		payment.setCash(totalRequired);
		updateRealCost();
		payment.updateDescription();
	}

	public void cancelPayment() {
		if (payment == null) {
			return;
		}
		AllCardPaymentDetail allCard = payment.getAllCardPaymentDetail();
		ProductCardPaymentDetail productCard = payment
				.getProductCardPaymentDetail();
		if (allCard != null) {
			AllCardPayment cardPayment = allCard.getCardPayment();
			float realCost = allCard.getRealCost();
			cardPayment.setMoney(cardPayment.getMoney() - realCost);
			cardPayment.setCurrentMoney(allCard.getCurrentLeftMoney()
					- realCost);
		} else if (productCard != null) {
			ProductCardPayment cardPayment = productCard.getCardPayment();
			float realCost = cardPayment.getRealCost();
			cardPayment.setMoney(cardPayment.getMoney() - realCost);
			cardPayment.setCurrentMoney(productCard.getCurrentLeftMoney()
					- realCost);
		} else {
			payment.setCash(0);
		}
		payment = null;
	}

	public float getRealCost() {
		return realCost;
	}

	public void setRealCost(float realCost) {
		this.realCost = realCost;
	}

	public float getRealCostTotal() {
		return realCostTotal;
	}

	public void setRealCostTotal(float realCostTotal) {
		this.realCostTotal = realCostTotal;
	}
}
